using System;
using System.Windows.Forms;

namespace CloudShot.ViewModels
{
  //TODO do we have a potential memleak? seems that through control subscribing. Control holds a reference to _holde through this class
  internal class ControlHoverHandler<TControl, TValue> where TControl : Control
  {
    private readonly IPropertyHolder<TControl, TValue> _holder;

    public TControl Control { get; private set; }
    public TValue PropertyValue { get; private set; }

    public ControlHoverHandler(TControl control, IPropertyHolder<TControl, TValue> holder, TValue propertyValue)
    {
      Control = control;
      _holder = holder;
      PropertyValue = propertyValue;

      control.MouseEnter += OnControlMauseEnter;
      control.MouseLeave += OnControlMauseLeave;
      control.Click += OnControlClick;
    }

    private void OnControlMauseEnter(object sender, EventArgs e)
    {
      _holder.PreviewProperty(Control, PropertyValue);
    }

    private void OnControlMauseLeave(object sender, EventArgs e)
    {
      _holder.ResetPreviewProperty(Control);
    }

    private void OnControlClick(object sender, EventArgs e)
    {
      _holder.SetProperty(Control, PropertyValue);
    }
  }
}